package com.feituan.manager.controller;

import com.feituan.core.pojo.request.PrinterRequest;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.service.IPrinterService;
import com.feituan.core.util.PrinterTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/printerTemplate")
public class PrinterTemplateController extends BaseController{

    @Autowired
    private PrinterTemplate printerTemplate;

    @Autowired
    private IPrinterService printerService;

    /**
     * 根据订单号和类型打印
     * @param orderSn orderSn
     * @param equipmentCodes equipmentCodes
     */
    @PostMapping("/printer")
    public Response<Boolean> printer(String orderSn, String equipmentCodes) {
        List<PrinterRequest> printerRequests = printerTemplate.templateEquipment(orderSn);
        //设备号list
        List<String> equipmentCodeList = Arrays.asList(equipmentCodes.split(","));
        // 过滤只保留equipmentCode在equipmentCodeList中的PrinterRequest对象
        List<PrinterRequest> filteredRequests = printerRequests.stream()
                .filter(request -> equipmentCodeList.contains(request.getEquipmentCode()))
                .collect(Collectors.toList());

        // 调用打印服务
        return Response.success(printerService.printer(filteredRequests));
    }
}
